<?php
namespace app\plus_exam\controller;
use app\plus_exam\model\School as SchoolModel;
use app\plus_exam\model\Category as CategoryModel;
/* *
 * 考场管理
 * */
class School extends Base{

    private $SchoolModel;
    private $CategoryModel;

    public function _initialize() {
        parent::_initialize();
        $this->SchoolModel = new SchoolModel;
        $this->CategoryModel = new CategoryModel;
    }

    //首页
    public function index(){
        $this->isAuth('plus_exam.school.index');
         //菜单开始
        $pathMaps = [
            ['name'=>'<i class="iconfont icon-home"></i> '.lang('lang_school_ui_title'),'url'=>url('plus_exam/school/index')],
        ];

        $editMenu = [
            ['name'=>'<i class="iconfont icon-cjsqm"></i> '.lang('lang_school_ui_add'),'url'=>url('plus_exam/school/add')]
        ];
        $this->assign('pathMaps',json_encode($pathMaps));
        $this->assign('editMenu',json_encode($editMenu));
        //菜单结束
        $info = $this->SchoolModel->select_lists();
        $page = $info->render();
        $this->assign('lists',$info);
        $this->assign('page',$page);
        return $this->fetch();
    }

    //添加
    public function add(){
        $this->isAuth('plus_exam.school.add');
        if(request()->isAjax()) {
            $data = [
                'category_id'     => input('post.category_id/s'),
                'name'            => input('post.name/s'),
                'start_time'      => input('post.start_time'),
                'end_time'        => input('post.end_time'),
                'is_repeat'       => input('post.is_repeat/d'),
                'lock_time'       => input('post.lock_time/d'),
                'start_lock_time' => input('post.start_lock_time/d'),
                'question_number' => input('post.question_number/d'),
                'one_point'       => input('post.one_point/d'),
            ];
            $res = $this->CategoryModel->find_info($data['category_id']);
            $data['category']=$res['name'];
            $validate = $this->validate($data,'school.add');
            if (!$validate) {
                return json(['code'=>1,'message'=>$validate]);
            }
            $result = $this->SchoolModel->add_data($data);
            if (!$result) {
                return json(['code'=>1,'message'=>lang('lang_message_error')]);
            }else{
                return json(['code'=>0,'message'=>lang('lang_message_success'),'data'=>['url' => url('plus_exam/school/index')]]);
            }

        }else{
            //菜单开始
            $pathMaps = [
                ['name'=>'<i class="iconfont icon-home"></i> '.lang('lang_school_ui_title'),'url'=>'javascript:;'],
            ];
            $editMenu = [];
            $this->assign('pathMaps',json_encode($pathMaps));
            $this->assign('editMenu',json_encode($editMenu));
            //菜单结束
            $category_list = $this->CategoryModel->select_lists();
            $this->assign('category_list',$category_list);
            return $this->fetch();
        }
    }

    //编辑
    public function edit(){
        $this->isAuth('plus_exam.school.edit');
        if (request()->isAjax()) {
            $data = [
                'id'              => input('post.id/d'),
                'category_id'     => input('post.category_id/d'),
                'name'            => input('post.name/s'),
                'start_time'      => input('post.start_time/s'),
                'end_time'        => input('post.end_time/s'),
                'is_repeat'       => input('post.is_repeat/d'),
                'lock_time'       => input('post.lock_time/d'),
                'start_lock_time' => input('post.start_lock_time/d'),
                'question_number' => input('post.question_number/d'),
                'one_point'       => input('post.one_point/d'),
            ];
            $res                = model('Category')->find_info($data['category_id']);
            $data['category']   = $res['name'];
            $validate           = $this->validate($data,'school.edit');
            if(true!== $validate){
                return json(['code' =>1,'message'=>$validate]);
            }
            $result = $this->SchoolModel->edit_data($data);
            if(!$result){
                return json(['code' =>1,'message'=>lang('lang_message_error')]);
            }else{
                return json(['code' =>0,'message'=>lang('lang_message_success'),'data'=>['url'=>url('plus_exam/school/index')]]);
            }
        } else {
           $pathMaps = [
                ['name'=>'<i class="iconfont icon-arrowleft"></i> '.lang('lang_ui_back'),'url'=>url('plus_exam/school/index')]
            ];
            $this->assign('pathMaps',json_encode($pathMaps));
            $editMenu = [];
            $this->assign('editMenu',json_encode($editMenu));
            //菜单结束
            $id   = input('id/d');
            $info = $this->SchoolModel->find_info($id);
            $info['start_time'] = date('Y-m-d H:i:s',$info['start_time']);
            $info['end_time'] = date('Y-m-d H:i:s',$info['end_time']);
            $this->assign('info',$info);
            $category_list = $this->CategoryModel->select_lists();
            $this->assign('category_list',$category_list);
            return $this->fetch();
        }
    }

    /**
     * 删除
     * @access public
     */
    public function delete() {
        $this->isAuth('plus_exam.school.delete');
        $id = input('id/d');
        $info = model('History')->find_info(['school_id' => $id]);
        if ($info) {
            return json(['code'=>1,'message'=>lang('lang_school_delete')]);
        }
        $res = $this->SchoolModel->delete_data($id);
        if (!$res) {
            return json(['code'=>1,'message'=>lang('lang_message_error')]);
        }else{
            return json(['code'=>0,'message'=>lang('lang_message_success')]);
        }
    }
}